<?php
public function getException(Acl $acl, IAclResource $resource, IAclRole $user, $privilege=null)
{
	//Entity resources get a simple 'access denied' exception
	if ( $acl->isChildOf($resource, 'entity') ) {
		return new AccessDeniedAclException($resource, $user);
	}

	$codes=array();
	array_push($codes, $resource->getResourceId());
	if ($this->isDataset($codes[0]))
	{
		$codes[0] = $this->normaliseDatasetSettings($codes[0]);
		array_push($codes,'legacy_'.$codes[0]);
	}

	//Tables
	$attributesTable = Schema::getAttributesTable();
	$siteSettingTable = Schema::getSiteSettingsTable();
	$sitesTable = Schema::getSitesTable();
			
	//Build query
	$codes=array_map(
			create_function('$code','return "\'" . pg_escape_string($code) . "\'";'),
			$codes);

	$queryCode=implode(",",$codes);

	$queryCode = '(' . $queryCode .')';
	$qry = "
	SELECT
		MAX(COALESCE(v.value,a.value)::int) AS value
	FROM
		$siteSettingTable v
		JOIN $sitesTable s ON s.id = v.site_id AND s.code = '$this->ident'
		RIGHT JOIN $attributesTable a ON a.id = v.attribute_id
	WHERE
		regexp_replace(a.code, '_available$', '') in $queryCode
		AND a.type_id = 1
		AND a.value_type_id = 2
	";

	$res = $this->db->query($qry); 
	$arr = $res->fetchArray();
	if ( !empty($arr) && $arr['value'] == 1 ) {
		return new UpsellAclException($resource, $user);
	}
	else {
		return new DisabledSiteFeatureAclException($resource, $user);
	}
}

	public function render() {
		
		// specify diffrent matching patterns based on report types.
		switch($this->mode) {
			case 'keystat':
				$patterns=array("/(?P<demoType>mosaic).*?(?P<subType>publicsector).*?(?P<grp>(group|type))/i",
								"/(?P<demoType>mosaic).*?(?P<grp>(group|type)).*?(?P<country>(uk|au)).*?(?P<year>\d+)breakdown/i",
								"/(?P<demoType>mosaic).*?(?P<grp>(group|type)).*?(?P<country>(uk|au|us|nz))breakdown/i",
								"/(?P<demoType>fss).*?(?P<subType>household).*?(?P<grp>(group|type))ukbreakdown/i",
								"/(?P<demoType>prizm).*?(?P<subType>(social|lifestage|segment))/i");
				break;
			case 'summary':
				$patterns=array("/(?P<demoType>mosaic).*?(?P<country>(uk|australia)).*?(?P<grp>(group|type)).*?(?P<year>\d+)/i",
								"/(?P<demoType>mosaic).*?(?P<country>(uk|australia)).*?(?P<grp>(group|type))/i",
								"/(?P<demoType>(prizm|fss)).*?(?P<subType>(social|lifestage|segment|household)).*?(?P<grp>(group|type))/i",
								"/(?P<demoType>mosaic).*?(?P<subType>public sector).*?(?P<grp>(group|type))/i",
								"/(?P<demoType>mosaic).*?(?P<grp>(group|type))/i",
								"/(?P<demoType>prizm).*?(?P<subType>(social|lifestage|segment))/i");
				break;
			default:
				$patterns=null;
		}
		if(!isset($patterns)) return '';
		
		// keep matching until a successful match hits
		// or all possible patterns have been tried.
		$patternIndex=0;
		preg_match($patterns[$patternIndex],$this->key,$matches);
		while(count($matches)==0 && $patternIndex<count($patterns)) {
			preg_match($patterns[$patternIndex],$this->key,$matches); 
			++$patternIndex;
		}
		
		// get lifestyle type
		$demoType=array_key_exists('demoType',$matches)? $matches['demoType']:'';
		// get lifestyle sub-type
		$subType='';
		if(array_key_exists('subType',$matches)) {
			if($matches['subType']=='Household') {
				$subType='hh';
			}
			else if(in_array($matches['subType'],array('PublicSector','Public Sector'))) {
				$subType='ps';
			}
			else {
				$subType=$matches['subType'];
			}
		}
		// get lifestyle group type
		$grp=array_key_exists('grp',$matches)? $matches['grp']:'';
		// get country information
		$country='';
		if(array_key_exists('country',$matches)) {
			if($matches['country']=='Australia') {
				$country='au';
			}
			else if(!in_array($matches['country'],array('US','NZ'))) {
				$country=$matches['country'];
			}
		}
		// get lifestyle product's year
		$year=array_key_exists('year',$matches)? $matches['year']:'';
		// build lifestyle type string
		$lifestyleType=strtolower($demoType . $country . $subType . $grp . $year);
		// au lifestyle product type naming convention is different
		if($lifestyleType=='mosaicaugroup') {
			$lifestyleType='mosaicaugroup2008';
		}
		if($lifestyleType=='mosaicautype') {
			$lifestyleType='mosaicautype2008';
		}
		
		// Look for matching lifestyle types and
		// returns corresponding third party information
		if(!is_array($this->lifestyleInfo))
		{
			$this->lifestyleInfo = array();
		}
		foreach(array_values($this->lifestyleInfo) as $productInfo) {
			if(in_array($lifestyleType,$productInfo['demtypes'])){
				return $productInfo['copyright'];
			}
		}
		return ''; 
	}
 }

/*
 * YUIDependenciesGrabber.php
 *
 * A utinity to grab YUI dependencies for a specified module
 * by scanning the corresponding javascript file.
 * 
 * Assuming dependencies are defined as "{requires:['node',[...'".
 * 
 * @usage
 * $grabber=new YUIDependenciesGrabber($path,"module");
 * $deps=grabber->getDependencies();
 *
 * @author jia.yuhua@hitwise.com
 * @version $Id$
 * @copyright &copy; 2011, Experian Hitwise
 */
class YUIDependenciesGrabber {
    private $deps;
    private $filePath;
    private $moduleName;
    
    /**
     * $file		Absolute path to the module javascript file.
     * $moduleName	The name of the module.
     */
    public function __construct($filePath,$moduleName) {
        if(!file_exists($filePath)) {
            trigger_error(__CLASS__ . " cannot locate $file",E_USER_WARNING);    
        }   
        else {
            $this->filePath=$filePath;
            $this->moduleName=$moduleName;
            $this->grab();
        }   
    }   
    
    /**
     * Return Module's dependencies names.
     * @return array
     */
    public function getDependencies() {
        if($this->deps==null)
            return array();
    
        return $this->deps; 
    }   

    /**
     * Scan the javascript file and match YUI dependency definition syntax
     * to get names of it's dependencies.
     * 
     * @return void
     */
    private function grab() {
        if(empty($this->filePath) || empty($this->moduleName)) {
            return;
        }   
    
        // Get module source code as string and strip off all white spaces. 
        $content=file_get_contents($this->filePath);    
        $normalizedContent=preg_replace('/\s*/','',$content);

        // Match YUI dependency definition syntax.
		$pattern='/YUI\.add\([\'\"]' . $this->moduleName . '[\'\"].*?\{requires:\[([^\]]*)\]\}?/';
        preg_match($pattern, $normalizedContent,$matches);
    
        // Put dependencies names in array.
        if(count($matches)>1 && !empty($matches[1])) {
             $depStr=preg_replace('/[\'\"]/', '', $matches[1]);
             $this->deps=explode(",", $depStr);
        }   
    }   
}


?>
